package com.hdhere.domain.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.data.domain.Page;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@ApiModel(value = "JqdtPagination", description = "jquery datatables分页类")
public class JqdtPagination<T> extends Pagination {

    @ApiModelProperty(value = "jq记录总数")
    private long recordsTotal;

    @ApiModelProperty(value = "jqtable控件使用")
    private long recordsFiltered;

    @ApiModelProperty(value = "jq起始记录数")
    private int iDisplayStart;

    @ApiModelProperty(value = "jq每页记录数")
    private int iDisplayLength;

    @ApiModelProperty(value = "jq第几列排序")
    private int iSortCol_0;

    @ApiModelProperty(value = "jq排序顺序 如：asc")
    private String sSortDir_0;

    //jqPage数据传到通用pagination中
    public void matchPage(){
        setPageSize(getiDisplayLength());
        setCurrentPage((int)Math.ceil(getiDisplayStart() / getiDisplayLength())+1);
        //设置排序顺序
        setOrder(getsSortDir_0());
        //取排序列名
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String sortProperty = request.getParameter("mDataProp_" + getiSortCol_0());
        setColumn(sortProperty);
    }

    @Override
    public void setPage(Page page) {
        super.setPage(page);
        setRecordsTotal(getTotalRecords());
        setRecordsFiltered(getTotalRecords());
    }

    public long getRecordsTotal() {
        return recordsTotal;
    }

    public void setRecordsTotal(long recordsTotal) {
        this.recordsTotal = recordsTotal;
    }

    public long getRecordsFiltered() {
        return recordsFiltered;
    }

    public void setRecordsFiltered(long recordsFiltered) {
        this.recordsFiltered = recordsFiltered;
    }

    public int getiDisplayStart() {
        return iDisplayStart;
    }

    public void setiDisplayStart(int iDisplayStart) {
        this.iDisplayStart = iDisplayStart;
    }

    public int getiDisplayLength() {
        return iDisplayLength;
    }

    public void setiDisplayLength(int iDisplayLength) {
        this.iDisplayLength = iDisplayLength;
    }

    public int getiSortCol_0() {
        return iSortCol_0;
    }

    public void setiSortCol_0(int iSortCol_0) {
        this.iSortCol_0 = iSortCol_0;
    }

    public String getsSortDir_0() {
        return sSortDir_0;
    }

    public void setsSortDir_0(String sSortDir_0) {
        this.sSortDir_0 = sSortDir_0;
    }
}
